//
//  TimerBar.m
//  cocos2dt1
//
//  Created by mmac on 1/19/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "constant.h"
#import "TimerBarLayer.h"
#import "LoseScene.h"



#define COUNT_TOTAL 600.0
#define BAR_FROM_X 190.0
#define BAR_TO_X 470.0
#define BAR_Y 304.0

@implementation TimerBarLayer

@synthesize countTotal;
@synthesize countCurrent;

- (void) startTimer {
	timerStopped = FALSE;
	
	self.countTotal = COUNT_TOTAL;		
	self.countCurrent = COUNT_TOTAL;
	
	[self schedule: @selector(timerTick:) interval:0.5];
}

- (void) stopTimer{
	timerStopped = TRUE;
	// [self deactivateTimers];
}

- (void) timesUp {
	[[Director sharedDirector] replaceScene: [PageTurnTransition transitionWithDuration:0.5 scene:[LoseScene scene] ]];

}

- (void) timerTick: (ccTime) dt{
	if(timerStopped){
		return;
	}
	
	// NSLog(@"tick %f", countCurrent);
	
	countCurrent--;
	if(countCurrent < 0){
		NSLog(@"time's up.");
		[self timesUp];
	}
	
	[self moveTimerCursor];
}

- (void) moveTimerCursor {
	// NSLog(@"%f/%f = %f", countCurrent, countTotal, countCurrent / countTotal);
	
	float div = (BAR_TO_X - BAR_FROM_X) * ( countCurrent / countTotal ) + BAR_FROM_X;
	// NSLog(@"cursor positon: %f,%f", div, BAR_Y);
	CGPoint divPoint = CGPointMake(div, BAR_Y);
	
	AtlasSpriteManager *mgr = (AtlasSpriteManager *)[self getChildByTag:kTagTimerCursorSpriteManager];
	AtlasSprite *cs = (AtlasSprite *)[mgr getChildByTag:kTagTimerCursorSprite];
	
	// NSLog(@"cursor %f,%f", cs.position.x, cs.position.y);
	cs.position = divPoint;
}

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
		
		AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"timer_cursor.png" capacity:50];
		[self addChild:mgr z:0 tag:kTagTimerCursorSpriteManager];
		
		AtlasSprite *cursorSprite = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 16, 16) spriteManager: mgr];
		
		AtlasAnimation *animation = [AtlasAnimation animationWithName:@"blink_cursor" delay:0.2f];
		for(int i=0;i<2;i++) {
			int x= i % 5;
			int y= i / 5;
			[animation addFrameWithRect: CGRectMake(x*16, y*16, 16, 16) ];
			
		}
		
		[mgr addChild:cursorSprite z:10 tag:kTagTimerCursorSprite];
		
		// CGSize s = [[Director sharedDirector] winSize];
		cursorSprite.position = ccp(BAR_TO_X , BAR_Y);
		// sprite2.position = ccp( s.width /2 - 100, s.height/2);
		//sprite3.position = ccp( s.width /2 + 100, s.height/2);
		
		id actionAnimation = [Animate actionWithAnimation: animation];
		id action = [RepeatForever actionWithAction:actionAnimation];
		
		
		// sprite.scale = 0.5f;
		[cursorSprite runAction:action];

		
		
		NSLog(@"timer init");
		
		[self startTimer];

	}
	
	return self;
}

- (void) draw {
	

	glLineWidth(8);
	// CGPoint vertices[] = { ccp(0,0), ccp(50,50), ccp(100,50), ccp(100,100), ccp(50,100) };
	
	int div = (BAR_TO_X - BAR_FROM_X) * ( countCurrent / countTotal ) + BAR_FROM_X;
	CGPoint divPoint = CGPointMake(div, BAR_Y);
	
	glColor4ub(0, 255, 0, 255);
	drawLine(CGPointMake(BAR_FROM_X, BAR_Y) , divPoint);

	
	glColor4ub(255, 0, 0, 255);
	drawLine(divPoint, CGPointMake( BAR_TO_X, BAR_Y));

	
	// restore original values
	glLineWidth(1);
	glColor4ub(255,255,255,255);
	glPointSize(1);	
	
}

@end
